package com.fanfanwang.ffw.app.tools;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.ArrayList;

import com.fanfanwang.ffw.app.AppConstant;


import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;


public class SharedPreferencesUtil {

	/**
	 * 文件名
	 */
	private static final String sh_data_name = "tinghaoche_gly_share_data";
	/**
	 * 是否登陆：true进入首页，false进入登陆页
	 */
	private static final String is_login = "is_login";
	/**
	 * 所属片区
	 */
	private static final String pianqu = "pianqu";
	/**
	 *停车场名称
	 */
	private static final String name = "name";
	/**
	 * 用户id
	 */
	private static final String parkingid = "parkingid";
	/**
	 * 经营人
	 */
	private static final String transactor = "transactor";
	
	/**
	 * 收费类型 ：0为按次数收费，1为按小时收费
	 */
	private static final String chargetype = "chargetype";
	
	/**
	 * 每次（小时）收费
	 */
	private static final String chargeprice = "chargeprice";
	/**
	 * 地址
	 */
	private static final String address = "address";
	/**
	 *收费时间段
	 */
	private static final String opentime = "opentime";
	/**
	 *  parktype：全日泊车
	 */
	private static final String parktype = "parktype";
	/**
	 * 默认启动第几页 ，fragment默认启动第几个
	 */
	private static final String  defalutFragment ="defalutFragment";
	
	private static SharedPreferences saveInfo;
	private static Editor saveEditor;
	private volatile static SharedPreferencesUtil uniqueInstance;

	
	

	
	
	private SharedPreferencesUtil() {
	}

	public static SharedPreferencesUtil getInstance(Context context) {
		if (uniqueInstance == null) {
			synchronized (SharedPreferencesUtil.class) {
				if (uniqueInstance == null) {
					uniqueInstance = new SharedPreferencesUtil();
					saveInfo = context.getSharedPreferences(sh_data_name, Context.MODE_PRIVATE);
					saveEditor = saveInfo.edit();
				}
			}
		}
		return uniqueInstance;
	}

	public int getDefalutFragment() {
		return saveInfo.getInt(defalutFragment, 0);
	}
	
	public boolean putDefalutFragment(int defaultFragment1) {
		saveEditor.putInt(defalutFragment, defaultFragment1);
		return saveEditor.commit();
	}
	public String getPainqu() {
		return saveInfo.getString(pianqu, null);
	}
	
	public boolean putPianqu(String pianqu1) {
		saveEditor.putString(pianqu, pianqu1);
		return saveEditor.commit();
	}


	
	
	public String getName() {
		return saveInfo.getString(name, null);
	}
	
	public boolean putName(String name1) {
		saveEditor.putString(name, name1);
		return saveEditor.commit();
	}
	public String getParkingId() {
		return saveInfo.getString(parkingid, null);
	}

	public boolean putParkingId(String id) {
		saveEditor.putString(parkingid, id);
		return saveEditor.commit();
	}
	public String getTransactor() {
		return saveInfo.getString(transactor, null);
	}
	
	public boolean putTransactor(String transactor1) {
		saveEditor.putString(transactor, transactor1);
		return saveEditor.commit();
	}
	public String getChargetype() {
		return saveInfo.getString(chargetype, null);
	}
	
	public boolean putChargetype(String chargetype1) {
		saveEditor.putString(chargetype, chargetype1);
		return saveEditor.commit();
	}
	public String getChargeprice() {
		return saveInfo.getString(chargeprice, null);
	}
	
	public boolean putChargeprice(String chargeprice1) {
		saveEditor.putString(chargeprice, chargeprice1);
		return saveEditor.commit();
	}
	public String getAddress() {
		return saveInfo.getString(address, null);
	}

	public boolean putAddress(String address1) {
		saveEditor.putString(address, address1);
		return saveEditor.commit();
	}
	public String getOpentime() {
		return saveInfo.getString(opentime, null);
	}

	public boolean putOpentime(String opentime1) {
		saveEditor.putString(opentime, opentime1);
		return saveEditor.commit();
	}
	public String getParktype() {
		return saveInfo.getString(parktype, null);
	}

	public boolean putParktype(String parktype1) {
		saveEditor.putString(parktype, parktype1);
		return saveEditor.commit();
	}

	/**
	 * 储存列表信息
	 * @param sKey
	 * @return
	 */
	public static boolean saveArray(ArrayList<String> sKey) {
		saveEditor.putInt("Status_size",sKey.size()); /*sKey is an array*/ 
		LogUtil.d("saveArray", "  >> " + sKey.size());
	    for(int i=0;i<sKey.size();i++) {
	    	saveEditor.remove("Status_" + i);
	    	saveEditor.putString("Status_" + i, sKey.get(i));  
	    }

	    return saveEditor.commit();     
	}
	
	/**
	 * 取列表信息
	 * @param mContext
	 * @param sKey
	 */
	public static void loadArray(Context mContext,ArrayList<String> sKey) {  
	    sKey.clear();
	    int size = saveInfo.getInt("Status_size", 0);  
	    LogUtil.d("loadArray", "  >> " + size);
	    for(int i=0;i<size;i++) {
	        sKey.add(saveInfo.getString("Status_" + i, null));  
	    }
	}
	

	/**
	 * @param key
	 * @param value
	 */
//	public void saveSerializable(String key, Object value) {
//		ByteArrayOutputStream baos = new ByteArrayOutputStream();
//		try {
//			ObjectOutputStream oos = new ObjectOutputStream(baos);
//			oos.writeObject(value);
//			oos.flush();
//			if (oos != null)
//				oos.close();
//			String strData = new String(Base64.encodeBase64(baos.toByteArray()));
//			saveEditor.putString(key.toLowerCase(), strData);
//			saveEditor.commit();
//		} catch (IOException e) {
//			e.printStackTrace();
//		} finally {
//			try {
//				if (baos != null)
//					baos.close();
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
//	}

//	/**
//	 * @param _key
//	 * @return
//	 */
//	public Object restoreSerializable(String _key) {
//		String key = _key.toLowerCase();
//		Object obj = null;
//		String objectInfo = saveInfo.getString(key, null);
//		if (objectInfo == null) {
//			return null;
//		}
//		ByteArrayInputStream bais = new ByteArrayInputStream(Base64.decodeBase64(objectInfo.getBytes()));
//		try {
//			ObjectInputStream ois = new ObjectInputStream(bais);
//			obj = ois.readObject();
//			ois.close();
//		} catch (StreamCorruptedException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		} catch (ClassNotFoundException e) {
//			e.printStackTrace();
//		} finally {
//			try {
//				if (bais != null)
//					bais.close();
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
//		return obj;
//	}
	public static void  putLoginDate(Context context,String loginDate){
		SharedPreferences sharedPreferences= context.getSharedPreferences("logindate", context.MODE_PRIVATE);
		Editor editor = sharedPreferences.edit();
		editor.putString("logindate", loginDate);
		editor.commit();
	}
	public static String getLoginDate(Context context){
		SharedPreferences sharedPreferences= context.getSharedPreferences("logindate", context.MODE_PRIVATE);
		return sharedPreferences.getString("logindate", "");
	}
}
